package com.web.server.controller.sse;

/**
 * Created by HUJING3 on 2017/4/1.
 */

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
@RequestMapping("/sse")
public class SSEDemo {

    private int COUNT = 0;

    @ResponseBody
    @RequestMapping("/message")
    public void message(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        try {
            int i=0;
            PrintWriter writer = response.getWriter();
            writer.write("data: 中文测试" + i++ + " \n\n");//这里需要两个\n
            writer.flush();
            System.out.println("====================");
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
